﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SqlCon;
using System.Data.SqlClient;

namespace yfxt
{
    public partial class zy_bqty : Form
    {
        DataTable dt = new DataTable();
        SqlConnection sqlcon;
        SqlDataAdapter da;
        SqlCommandBuilder scdb;
        int zyh, ypxh;
        string constr = "Data Source=localhost;database=yf;uid=sa;pwd=123;timeout=60";
        public zy_bqty()
        {
            InitializeComponent();
        }

        private void zy_bqty_Load(object sender, EventArgs e)
        {
            button3.Enabled = false;
            button4.Enabled = false;

            DataTable dt_bq = new DataTable();
            string sqlstr1 = "select ksdm,ksmc from GY_KSDM where BQSY='Y'";
            dt_bq.Load(SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, sqlstr1, null));
            comboBox1.DataSource = dt_bq;
            comboBox1.DisplayMember = "ksmc";
            comboBox1.ValueMember = "ksdm";
        }

        #region 退出
        private void button6_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion

        #region 刷新，获取退药信息
        private void button1_Click(object sender, EventArgs e)
        {
            #region 获取科室代码
            int ksdm;
            DataTable dt_ksdm = new DataTable();
            string sqlksdm = "select ksdm from gy_ksdm where ksmc='" + comboBox1.Text.ToString().Trim() +"'";
            dt_ksdm.Load(SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction,CommandType.Text,sqlksdm,null));
            ksdm = int.Parse(dt_ksdm.Rows[0]["ksdm"].ToString());
            #endregion
            dt.Clear();
            string sql_tystr = "select b.brch,b.brxm,c.ypmc,a.ypgg,a.yfdw,a.ypsl,a.ypjg,(a.ypsl*a.ypjg) as zjje,d.cdmc,a.zyh,a.ypxh from "+
                "bq_tymx a,"+
                "zy_brry b,"+
                "yk_typk c, "+
                "yk_cddz d " +
                "where a.tjbz=1 "+
                "and a.zfpb=0 "+
                "and tybq='"+ksdm+"' "+
                "and a.yfsb='"+mainform.yfsb+"' "+
                "and a.ypxh=c.ypxh "+
                "and a.zyh=b.zyh "+
                "and a.ypcd=d.ypcd "+
                "and CONVERT(varchar(25), sqrq, 121) like '"+dateTimePicker1.Value.ToShortDateString() +"%' ";

            sqlcon = new SqlConnection(constr);
            sqlcon.Open();
            da = new SqlDataAdapter(sql_tystr,constr);
            scdb = new SqlCommandBuilder(da);
            try
            {
                da.Fill(dt);
                dataGridView1.Columns[0].DataPropertyName = dt.Columns[0].ToString();
                dataGridView1.Columns[1].DataPropertyName = dt.Columns[1].ToString();
                dataGridView1.Columns[2].DataPropertyName = dt.Columns[2].ToString();
                dataGridView1.Columns[3].DataPropertyName = dt.Columns[3].ToString();
                dataGridView1.Columns[4].DataPropertyName = dt.Columns[4].ToString();
                dataGridView1.Columns[5].DataPropertyName = dt.Columns[5].ToString();
                dataGridView1.Columns[6].DataPropertyName = dt.Columns[6].ToString();
                dataGridView1.Columns[7].DataPropertyName = dt.Columns[7].ToString();
                dataGridView1.Columns[8].DataPropertyName = dt.Columns[8].ToString();
                dataGridView1.Columns[9].DataPropertyName = dt.Columns[9].ToString();
                dataGridView1.Columns[10].DataPropertyName = dt.Columns[10].ToString();
                dataGridView1.DataSource = dt;
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.ToString());
            };
            try 
            {
                dataGridView2.Columns[0].DataPropertyName = dt.Columns[0].ToString();
                dataGridView2.Columns[1].DataPropertyName = dt.Columns[1].ToString();
                dataGridView2.Columns[2].DataPropertyName = dt.Columns[2].ToString();
                dataGridView2.Columns[3].DataPropertyName = dt.Columns[3].ToString();
                dataGridView2.Columns[4].DataPropertyName = dt.Columns[4].ToString();
                dataGridView2.Columns[5].DataPropertyName = dt.Columns[5].ToString();
                dataGridView2.Columns[6].DataPropertyName = dt.Columns[6].ToString();
                dataGridView2.Columns[7].DataPropertyName = dt.Columns[7].ToString();
                dataGridView2.Columns[8].DataPropertyName = dt.Columns[8].ToString();
                dataGridView2.Columns[9].DataPropertyName = dt.Columns[9].ToString();
                dataGridView2.Columns[10].DataPropertyName = dt.Columns[10].ToString();
                dataGridView2.DataSource = dt;
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.ToString());
            };

        }
        #endregion

        #region 退药
        private void button2_Click(object sender, EventArgs e)
        {
            DataTable dt_ypsl = new DataTable();
            int rowcount=0;
            string sql_kcupdate,sql_tyupdate,sql_kcsl;
            float kc_ypsl;
            float ty_ypsl;
            for (rowcount = 0; rowcount < dataGridView1.RowCount;rowcount++ )
            {
                ypxh = int.Parse(dt.Rows[rowcount]["ypxh"].ToString());
                zyh = int.Parse(dt.Rows[rowcount]["zyh"].ToString());
                ty_ypsl = float.Parse(dt.Rows[rowcount]["ypsl"].ToString());
                sql_kcsl = "select ypsl from yf_kcmx where ypxh='" + ypxh + "'";
                dt_ypsl.Load(SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction,CommandType.Text,sql_kcsl,null));
                kc_ypsl = float.Parse(dt_ypsl.Rows[0]["ypsl"].ToString());
                SqlParameter[] para2 = new SqlParameter[]
                {
                    new SqlParameter("@kc_ypsl",kc_ypsl),
                    new SqlParameter("@ty_ypsl",ty_ypsl),
                    new SqlParameter("@ypxh",ypxh)
                };
                SqlParameter[] para1 = new SqlParameter[]
                {
                    new SqlParameter("@zyh",zyh),
                    new SqlParameter("@ypxh",ypxh)
                };
                sql_kcupdate = "update yf_kcmx set ypsl=(@kc_ypsl-@ty_ypsl) where ypxh=@ypxh and yfsb='" + mainform.yfsb + "'";
                sql_tyupdate = "update bq_tymx set tjbz=2 where zyh=@zyh and ypxh=@ypxh";
                SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringLocalTransaction,CommandType.Text,sql_kcupdate,para2);
                SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringLocalTransaction,CommandType.Text,sql_tyupdate,para1);
            }
            button1_Click(sender,e);
            MessageBox.Show("退药成功");

        }
        #endregion

        #region tabcontrol点击事件
        private void tabControl1_Click(object sender, EventArgs e)
        {
            if(tabControl1.SelectedTab==tabPage1)
            {
                button2.Enabled = true;
                button3.Enabled = false;
                button4.Enabled = false;
            }
            if(tabControl1.SelectedTab==tabPage2)
            {
                button2.Enabled = false;
                button3.Enabled = true;
                button4.Enabled = true;
            }
        }
        #endregion

        #region 退回病区
        private void button4_Click(object sender, EventArgs e)
        {
            int rowindex,res;
            string sqlupdate;
            rowindex = dataGridView2.CurrentRow.Index;
            zyh = int.Parse(dt.Rows[rowindex]["zyh"].ToString());
            ypxh = int.Parse(dt.Rows[rowindex]["ypxh"].ToString());

            SqlParameter[] para = new SqlParameter[]
            {
                new SqlParameter("@zyh",zyh),
                new SqlParameter("@ypxh",ypxh)
            };
            sqlupdate = "update bq_tymx set tjbz=0 where zyh=@zyh and ypxh=@ypxh ";
            try
            {
                
                res=SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringLocalTransaction,CommandType.Text,sqlupdate,para);
                //da.Update(dt);
                if (res != 0) 
                {
                    button1_Click(sender,e);
                    MessageBox.Show("退回病区成功");
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            
        }
        #endregion

        #region 全部退回
        private void button3_Click(object sender, EventArgs e)
        {
            string sqlupdate;
            int rowcount=0;
            //rowcout = dataGridView2.RowCount;
            try
            {
                for (rowcount=0; rowcount < dataGridView2.RowCount;rowcount++ )
                {
                    zyh = int.Parse(dt.Rows[rowcount]["zyh"].ToString());
                    ypxh = int.Parse(dt.Rows[rowcount]["ypxh"].ToString());
                    sqlupdate = "update bq_tymx set tjbz=0 where zyh=@zyh and ypxh=@ypxh ";
                    SqlParameter[] para = new SqlParameter[]
                    {
                        new SqlParameter("@zyh",zyh),
                        new SqlParameter("@ypxh",ypxh)
                    };
                    SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, sqlupdate, para);
                }
                button1_Click(sender, e);
                MessageBox.Show("全部退回成功");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
        #endregion
    }
}
